Ext.BLANK_IMAGE_URL = basePath + 'ext/resources/images/default/s.gif';
Ext.SSL_SECURE_URL = basePath + 'ext/resources/images/default/s.gif';

/**
 * 定义页面对应的后台处理类完整路径。
 */
var clsUrl = basePath + 'JarpListener?className=cn.com.zsc.system.SelectPropertyType';
var status;

Ext.onReady(function(){
	Ext.QuickTips.init();
	
	/**
	 * 状态数据源
	 */
	var statusStore=new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : clsUrl + '&methodName=getComboBox&paramCount=1&param_0=4'
		}),
		reader : new Ext.data.JsonReader({
			fields : [
			{name:'CL_CODE'},
			{name:'CL_NAME'}
		]
		}),
		autoLoad : true
	});
	
	/**
	 * 状态控件
	 */
	var statusComboBox = new Ext.form.ComboBox({
		fieldLabel : '状态',
		id : 'status',
		width : 200,
		displayField : 'CL_NAME',//text id : 'status'
		valueField : 'CL_CODE',//value
		emptyText : '请选择',
		store:statusStore,
		mode : 'local',
		editable  : false,
		triggerAction : 'all',
		selectOnFocus : true
	});
	
	var storereader= new Ext.data.JsonReader({
		//为分页增加
		root : 'results',
        totalProperty : 'totalCount',
        
    	fields:[
    		{name :'plCode'},
    		{name :'plName'},
    		{name :'status'},
    		{name :'remarked'}
    	]
	});
	
	var store = new Ext.data.Store({
		reader:storereader,
		proxy:new Ext.data.HttpProxy({
			url:clsUrl+'&methodName=getPropertyType&paramCount=0'
		})
	});
	store.load();
	var toolpanel = new Ext.PagingToolbar({
                    pageSize:20,
                    store:store,
                    storeParams : {
                            className : 'cn.com.zsc.system.SelectPropertyType',
                            methodName : 'getPropertyType',
                            paramCount : '2',
                            param_0 : '0',
                            param_1 : '20'
                        },
                        paramNames : {
                            start : 'param_0',
                            limit : 'param_1'
                        },
                    displayInfo: true,
                    displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
                    emptyMsg: '没有记录'
                });
				
	var basegrid = new Ext.grid.GridPanel( {
		id :'basegrid',
		store :store,
		split :true,
		bodyStyle:'5px 15px 0',
		region :'center',
		border :false,
		enableColumnMove:false,                                           
	  	enableHdMenu:false,
		frame :false,
		autoScroll :true,
		viewConfig: {
        	forceFit: true
    	},
    	columns: [
		            new Ext.grid.RowNumberer(),
    	          	{id:'plCode',name:'plCode', header:'编号',enableColumnMove:false,sortable:true,hidden:true,hideCollapseTool:false, enableHdMenu:false,enableColumnResize :false,dataIndex: 'plCode'},
    	          	{id:'plName',name:'plName', header:'名称',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'plName'},
//    	          	{id:'status',name:'status',header:'状态',sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'status'},
                    {
                   	 header :'状态',
               		 sortable : true,
               		 locked : false,
               		 dataIndex:'status',
               		 renderer:function (val){
                                    if (val == '1'){
                                        return '启用';
                                    }else{
                                        return '停用';
                                    }
                                }
           			 } ,
    	          	{id:'remarked',name:'remarked',header:'备注',sortable:true,hidden:false,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false,dataIndex: 'remarked'}
    	],
    	tbar:[
	    	     {
		    	     text:'新增',
		    	     iconCls:'add',
		      	   	 handler:function(){
		      	   	 	status=1;
		      	   	 	win.setTitle("新增");
		      	   	 	win.show();
//		      	   	 	Ext.getCmp("plCode").disable();
		      	   	 	Ext.Ajax.request({
		      	   	 		url:clsUrl+'&methodName=getPropertyType&&paramCount=0',
		      	   	 		success:function(req){
		      	   	 			Ext.getCmp("plCode").setValue(req.responseText);
		      	   	 		},
		      	   	 		failure:function(req){
		      	   	 		}
		      	   	 	});
//						Ext.getCmp("plCode").setValue("");
						Ext.getCmp("plName").setValue("");
						Ext.getCmp("status").setValue("");
						Ext.getCmp("remarked").setValue("");
		    	     }
	      	   	},
	      	   	'-',
	      	   	{
		      	   	text:'修改',
		      	   	iconCls:'modify',
		      	   	handler:function(){
	      	   		status=2;
	      	   		var record = basegrid.getSelectionModel().getSelected(); 
	   		        if(!record){   
			   		   Ext.Msg.show({   
			   		     title: '提示',
			   		     width:210,
			   		     msg: '请选择要修改的数据！',   
			   		     buttons : Ext.Msg.OK,   
			   		     icon : Ext.Msg.ERROR   
			   		    });      
		      	   	}else{
			      	   	if(record){   
		            	   	win.show();
		            	   	win.setTitle("修改");
//		            	   	Ext.getCmp("plCode").disable();
		            	   	var record = basegrid.getSelectionModel().getSelected();
							Ext.getCmp("plCode").setValue(record.data.plCode);
							Ext.getCmp("plName").setValue(record.data.plName);
							
							Ext.getCmp("status").setValue(record.data.status);
							Ext.getCmp("remarked").setValue(record.data.remarked);
			              }     
		      	   		}
	      	   		}
	      	   	},
	      	   	'-',
	      	   	{
		      	   	text:'删除',
		      	   	iconCls:'delete',
		      	   	handler:del
	      	   	}
	      	   	
 	  ],
 	  bbar:toolpanel
	});
	
	var formPanel = new Ext.FormPanel({
		bodyStyle:'padding:15px 15px 0',
		region:'center',
		layout:'form',
		frame:true,
		labelWidth:60,
		labelAlign:'right',
		items:[
		
		       {fieldLabel: '编号',width:200,name: 'plCode',id:'plCode',xtype:'textfield',hideLabel:true,hidden:true},
		       {fieldLabel: '名称',width:200,name: 'plName',id:'plName',xtype:'textfield'},
//		       {fieldLabel: '状态',width:200,name: 'status',id:'status',xtype:'textfield'},
				statusComboBox,
		       {fieldLabel: '备注',width:200,height:80,name: 'remarked',id:'remarked',xtype:'textarea'}		       
		]
	});
	
	var win = new Ext.Window({
		title:'新增',
		width:350,
		height:250,
		layout:'border',
		closeAction:'hide',
		modal:true,
		draggable:false,
		resizable:false,
		tbar:[
		      {
		    	  text:'保存',
		    	  iconCls:'save',
		    	  handler:judge
		      },
		      '-'
		      ,
		      {
		    	  text:'取消',
		    	  iconCls:'cancel',
		    	  handler:function(){
      	   			win.hide();
		      	  }
		      }
		],
		items:[formPanel]
		
	});
	
	var viewport = new Ext.Viewport( {
		layout:'border',
		items: [basegrid ]
	});
	
	function judge(){
		if(status==1){
			save();
		}else if(status==2){
			update();
		}
	}
	
	function save(){
		if (!saveValidate()) {
			return;
		}
		var obj = new Object();
		obj.plName = Ext.getCmp("plName").getValue();
		obj.status = Ext.getCmp("status").getValue();
		obj.remarked = Ext.getCmp("remarked").getValue();
		
		var url=clsUrl+'&methodName=insertProptype&paramCount=0'
    	httpReq=null;
		httpReq = getXMLHttpRequest();
    	httpReq.onreadystatechange = onSaveRtn;
		httpReq.open("POST",url, true);
		httpReq.send(Ext.util.JSON.encode(obj));
	}
	
	function saveValidate(){
		if(Ext.getCmp("plName").getValue()==""){
			Ext.getCmp("plName").markInvalid("名称不能为空");
			return false;
		}
		return true;
	}
	
	function onSaveRtn(){
		if(httpReq.readyState == 4){
		   	if(httpReq.status == 200){
		   		if(httpReq.responseText == "success"){
		   			Ext.Msg.show({   
		    	   		title : '成功提示',
		    	   		width:200,
		   				msg : '保存成功！',  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.INFO   
		   			});
		   			store.reload();
		   			Ext.getCmp("plCode").setValue("");
		   			Ext.getCmp("plName").setValue("");
		   			Ext.getCmp("status").setValue("");
		   			Ext.getCmp("remarked").setValue("");
		   			win.hide();
		   			httpReq=null;
		   		}else{
		   			Ext.Msg.show({   
		    	   		title : '错误提示',
		    	   		width:200,
		   				msg : httpReq.responseText,  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.ERROR   
		   			});  
		   			httpReq=null;
		   		}
		   	}
		}
	}
	
	function del(){
		var record = basegrid.getSelectionModel().getSelected();
		if(!record){
			Ext.Msg.show({
				title:'提示',
				width:210,
				msg:'请选择要删除的数据',
				buttons:Ext.Msg.OK,
				icon: Ext.Msg.ERROR 
			});	
		}	
		if(record){
			Ext.Msg.show({
				title:'提示',
				width:200,
				msg:'确定要删除数据！', 
				buttons: Ext.Msg.YESNO,
	            icon: Ext.MessageBox.QUESTION,
				fn:function(btn){
					if( "yes" == btn){
						delProc()
					}
				}
			});
			
		}
					
	}
	
		function delProc(){
		var record = basegrid.getSelectionModel().getSelected(); 
		var obj = new Object();
		obj.plCode = record.data.plCode;
		var url=clsUrl+'&methodName=delPropType&paramCount=0'
    	httpReq=null;
		httpReq = getXMLHttpRequest();
    	httpReq.onreadystatechange = onDelRtn;
		httpReq.open("POST",url, true);
		httpReq.send(Ext.util.JSON.encode(obj));
	}
	
	function onDelRtn(){
		if(httpReq.readyState == 4){
		   	if(httpReq.status == 200){
		   		if(httpReq.responseText == "success"){
		   			Ext.Msg.show({   
		    	   		title : '成功提示',   
		   				msg : '删除成功！',  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.INFO   
		   			});
		   			store.reload();
		   			Ext.getCmp("plCode").setValue("");
		   			httpReq=null;
		   		}else{
		   			Ext.Msg.show({   
		    	   		title : '错误提示',   
		   				msg : '保存失败，发生错误：'+httpReq.responseText,  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.ERROR   
		   			});  
		   			httpReq=null;
		   		}
		   	}
		}
	}
	
	function update(){
		if(!saveValidate()){
			return ;
		}
		var obj = new Object();
		var record = basegrid.getSelectionModel().getSelected();
		obj.plCode = Ext.getCmp("plCode").getValue();
		obj.plName = Ext.getCmp("plName").getValue();
		obj.status = Ext.getCmp("status").getValue();
		obj.remarked = Ext.getCmp("remarked").getValue();		
		var url=clsUrl+'&methodName=updateType&paramCount=0'
    	httpReq=null;
		httpReq = getXMLHttpRequest();
    	httpReq.onreadystatechange = onUpdateRtn;
		httpReq.open("POST",url, true);
		httpReq.send(Ext.util.JSON.encode(obj));
	}
	
	function onUpdateRtn(){
		if(httpReq.readyState == 4){
		   	if(httpReq.status == 200){
		   		if(httpReq.responseText == "success"){
		   			Ext.Msg.show({   
		    	   		title : '成功提示',   
		   				msg : '保存成功！',  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.INFO   
		   			});
		   			store.reload();
		   			Ext.getCmp("plCode").setValue("");		   			
		   			Ext.getCmp("plName").setValue("");
		   			Ext.getCmp("status").setValue("");
		   			Ext.getCmp("remarked").setValue("");
		   			win.hide();
		   			httpReq=null;
		   		}else{
		   			Ext.Msg.show({   
		    	   		title : '错误提示',   
		   				msg : '保存失败，发生错误：'+httpReq.responseText,  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.ERROR   
		   			});  
		   			httpReq=null;
		   		}
		   	}
		}
	}
	
	});